package com.shadowtorcher.stdisk.util;

import com.shadowtorcher.soulknife.SoulKnifeDB;
import com.shadowtorcher.soulknife.exception.BeanConfigErrorException;
import com.shadowtorcher.stdisk.bean.PermissionBean;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;

public class Permission {
    private Map<String, String> data = new HashMap<>();

    public Permission(PermissionBean bean) {
        String permissions = bean.getPermissions();
        String[] strs = permissions.split(",");
        for (String s : strs) {
            String[] ss = s.split("=");
            data.put(ss[0], ss[1]);
        }
    }

    public Permission(String uuid) throws SQLException, BeanConfigErrorException {
        this(SoulKnifeDB.selectWhere("PermissionID = ?", uuid).setClass(PermissionBean.class).get(0));
    }

    public String get(String s) {
        return data.get(s);
    }

    public String toString() {
        StringJoiner stringJoiner = new StringJoiner(",");
        for (Map.Entry<String, String> entry : data.entrySet()) {
            String s = entry.getKey() + "=" + entry.getValue();
            stringJoiner.add(s);
        }
        return stringJoiner.toString();
    }
}
